package com.huluwa.picture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huluwa.common.exception.ServiceException;
import com.huluwa.picture.common.BaseResponse;
import com.huluwa.picture.common.DeleteRequest;
import com.huluwa.picture.common.ResultUtils;
import com.huluwa.picture.exception.ErrorCode;
import com.huluwa.picture.exception.ThrowUtils;
import com.huluwa.picture.model.dto.webcollection.WebCollectionAddReqeuest;
import com.huluwa.picture.model.dto.webcollection.WebCollectionEditReqeuest;
import com.huluwa.picture.model.dto.webcollection.WebCollectionQueryRequest;
import com.huluwa.picture.model.entity.WebCollection;
import com.huluwa.picture.model.vo.WebCollectionVO;
import com.huluwa.picture.service.WebCollectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Classname WebCollectionController
 * @Date 2025-02-01 0:13
 * @Created by luyifan
 */

@RestController
@RequestMapping("/webcollection")
public class WebCollectionController {
    @Resource
    private WebCollectionService webCollectionService;

    /**
     * 添加网站
     */
    @PostMapping("/add")
    public BaseResponse<Integer> addWebCollection(@RequestBody WebCollectionAddReqeuest webCollectionAddReqeuest) {
        ThrowUtils.throwIf(webCollectionAddReqeuest == null, ErrorCode.PARAMS_ERROR);
        Integer webCollectionId = webCollectionService.addWebCollection(webCollectionAddReqeuest);
        return ResultUtils.success(webCollectionId);
    }

    /**
     * 删除网站
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteWebCollection(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR.getMessage(),ErrorCode.PARAMS_ERROR.getCode());
        }
        WebCollection oldWebCollection = webCollectionService.getById(deleteRequest.getId());
        ThrowUtils.throwIf(oldWebCollection == null, ErrorCode.NOT_FOUND_ERROR);
        boolean reuslt = webCollectionService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!reuslt ,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 编辑网站
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> editWebCollection(@RequestBody WebCollectionEditReqeuest webCollectionEditReqeuest) {
        if (webCollectionEditReqeuest == null || webCollectionEditReqeuest.getId() <= 0) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR.getMessage(),ErrorCode.PARAMS_ERROR.getCode());
        }
        WebCollection webCollection = new WebCollection();
        BeanUtils.copyProperties(webCollectionEditReqeuest, webCollection);
        boolean result = webCollectionService.updateById(webCollection);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 获取网站详情
     */
    @PostMapping("/get")
    public BaseResponse<WebCollection> getWebCollection(@RequestBody WebCollectionQueryRequest webCollectionQueryRequest) {
        // 参数校验
        ThrowUtils.throwIf(webCollectionQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Long webCollectionId = webCollectionQueryRequest.getId();
        ThrowUtils.throwIf(webCollectionId == null || webCollectionId <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        WebCollection webCollection = webCollectionService.getOne(webCollectionService.getSearchQueryWrapper(webCollectionQueryRequest));
        ThrowUtils.throwIf(webCollection == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(webCollection);
    }


    /**
     * 获取网站详情(包装类)
     */
    @PostMapping("/get/vo")
    public BaseResponse<WebCollectionVO> getWebCollectionVO(@RequestBody WebCollectionQueryRequest webCollectionQueryRequest) {
        // 参数校验
        ThrowUtils.throwIf(webCollectionQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Long webCollectionId = webCollectionQueryRequest.getId();
        ThrowUtils.throwIf(webCollectionId == null || webCollectionId <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        WebCollection webCollection = webCollectionService.getOne(webCollectionService.getSearchQueryWrapper(webCollectionQueryRequest));
        ThrowUtils.throwIf(webCollection == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(webCollectionService.getWebCollectionVO(webCollection));
    }


    /**
     * 分页网站详情 (仅管理员使用)
     */
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping("/list/page")
    public BaseResponse<Page<WebCollection>> listWebCollection(@RequestBody WebCollectionQueryRequest webCollectionQueryRequest) {
        int current = webCollectionQueryRequest.getCurrent();
        int pageSize = webCollectionQueryRequest.getPageSize();
        Page<WebCollection> page = webCollectionService.page(new Page<WebCollection>(current, pageSize), webCollectionService.getSearchQueryWrapper(webCollectionQueryRequest));
        return ResultUtils.success(page);
    }

    /**
     * 分页获取网站详情 (包装类)
     * @param webCollectionQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<WebCollectionVO>> listWebCollectionVOPage(@RequestBody WebCollectionQueryRequest webCollectionQueryRequest) {
        int current = webCollectionQueryRequest.getCurrent();
        int pageSize = webCollectionQueryRequest.getPageSize();
        Page<WebCollection> page = webCollectionService.page(new Page<WebCollection>(current, pageSize), webCollectionService.getSearchQueryWrapper(webCollectionQueryRequest));
        return ResultUtils.success(webCollectionService.getWebCollectionPage(page));
    }
}
